var net          = require('net');

var server       = net.createServer();

var client_List  = [];

var channel_list = [];

var word_channel = [];

function init() {
	channel_list.push(word_channel);
}


server.on('connection', function(client) {
	console.log('connected:' + client.remoteAddress + ':' + client.remotePort);

	client_List.push(client);

	client.on('data', function(data) {
		var msg = JSON.parse(data);

		if(msg.mc == 'join') {

		} else if(msg.mc == 'logout') {

		} else if(msg.mc == 'login') {
			client.name = msg.name;
			client.cid = msg.id;

			console.log(client.name + ' login');

			//login auto join word channel
			word_channel.push(client);

		} else if(msg.mc == 'say') {
			console.log(client.name + ' say:' + msg.msg);
		} else {
			console.log(client.name + ' rec:' + data);
		}
	})


	client.on('end', function() {
		//disconnected out all channel
		//word_channel
		word_channel.splice(client_List.indexOf(client), 1);
		console.log(client.name + '  disconnected');
	});
})


server.on('error', function(e) {
	console.log(e);
})

server.listen(9001);